/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.Model;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 *
 * @author Paulo Ricardo
 */
public abstract class Funcoes {

    // String para data
    public static Date StringToDate(String data) throws Exception {

        // String data = "Nov 22 00:00:00 BRST 2011";
        String pattern = "EEE MMM dd HH:mm:ss zzzz yyyy";
        // String pattern = "dd zzzz yyyys HH:mm:ss";

        SimpleDateFormat df = new SimpleDateFormat(pattern, Locale.US);
        Date date = df.parse(data);

        return date;
        //	return new Date();
    }

    public static Date StringToDate_DDMMYYYY(String dataExpiracao) {

        String pattern = "dd/MM/yyyy";
        Date Data;
        // String pattern = "dd zzzz yyyys HH:mm:ss";

        SimpleDateFormat df = new SimpleDateFormat(pattern, Locale.US);

        try {
            Data = df.parse(dataExpiracao);
            return Data;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;

    }

    // Data para String
    public static String DateToString(Date pdata) {

        SimpleDateFormat dateformat = new SimpleDateFormat(
                "EEE MMM dd HH:mm:ss zzzz yyyy", Locale.US);


        Calendar calendar = Calendar.getInstance();
        calendar.setTime(pdata);

        return dateformat.format(calendar.getTime());
        //	return "21/07/1993";
    }

    public static double ArredondarDouble(double Numero) {

        BigDecimal ObjBidDecimal = new BigDecimal(Numero);
        ObjBidDecimal = ObjBidDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);

        return ObjBidDecimal.doubleValue();

    }

    public static Date addDias(int Dias, Date Data) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        // incrementa minha data  
        calendar.add(Calendar.DAY_OF_MONTH, Dias);
        try {
           return Data = StringToDate(dateFormat.format(calendar.getTime()));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        return Data;

    }
}
